<?php
/**
 * @author yuanyitian
 * @date 2024/8/14 7:15
 */

namespace api\admin;

class pre
{
    public function changeStatus($request)
    {
        $id = (int)$request['id'];
        $status = (int)$request['status'];
        $time = time();
        $con = con();
        $res = mysqli_query($con, "update pre set status=$status,update_time=$time where id=$id");
        mysqli_close($con);
        if ($res) {
            return out_right();
        } else {
            return out_error();
        }
    }

    public function del($request)
    {
        $id = (int)$request['id'];
        $time = time();
        $con = con();
        $res = mysqli_query($con, "update pre set is_delete=0,delete_time=$time where id=$id");
        mysqli_close($con);
        if ($res) {
            return out_right();
        } else {
            return out_error();
        }
    }

    public function edit($request)
    {
        date_default_timezone_set('Asia/Shanghai');
        $today_time = strtotime(date('Y-m-d'));
        $id = (int)$request['id'];
        $name = $request['name'];
        $date = implode(',', $request['date']);
        $remark = $request['remark'];
        $time = time();

        $price_1=$request['price_1'] * 100;
        $price_2=$request['price_2'] * 100;
        $price_3=$request['price_3'] * 100;
        $price_vip_1=$request['price_vip_1'] * 100;
        $price_vip_2=$request['price_vip_2'] * 100;
        $price_vip_3=$request['price_vip_3'] * 100;

        $con = con();
        $res = mysqli_query($con, "update pre set name='$name',date='$date',remark='$remark',update_time=$time,price_1=$price_1,price_2=$price_2,price_3=$price_3,price_vip_1=$price_vip_1,price_vip_2=$price_vip_2,price_vip_3=$price_vip_3 where id=$id");
        mysqli_close($con);
        if ($res) {
            return out_right();
        } else {
            return out_error();
        }

    }

    public function add($request)
    {
        date_default_timezone_set('Asia/Shanghai');
        $name = $request['name'];
        $remark = $request['remark'];
        $date = implode(',', $request['date']);
        $time = time();

        $price_1=$request['price_1'] * 100;
        $price_2=$request['price_2'] * 100;
        $price_3=$request['price_3'] * 100;
        $price_vip_1=$request['price_vip_1'] * 100;
        $price_vip_2=$request['price_vip_2'] * 100;
        $price_vip_3=$request['price_vip_3'] * 100;

        $con = con();
        $res = mysqli_query($con, "insert into pre(name,date,remark,create_time,update_time,price_1,price_2,price_3,price_vip_1,price_vip_2,price_vip_3) values('$name','$date','$remark',$time,$time,$price_1,$price_2,$price_3,$price_vip_1,$price_vip_2,$price_vip_3)");
        $res = mysqli_insert_id($con);
        if (!$res) {
            return out_error('添加失败！');
        }
        $id = (int)$res;
        $type_1 = $request['type_1'];
        $type_2 = $request['type_2'];
        $type_3 = $request['type_3'];
        for ($i = 0; $i < count($type_1); $i++) {
            $type_id = (int)$type_1[$i];
            mysqli_query($con, "insert into pre_goods_config(menu_id,goods_id,type,create_time) values($id,$type_id,1,$time)");
        }
        for ($i = 0; $i < count($type_2); $i++) {
            $type_id = (int)$type_2[$i];
            mysqli_query($con, "insert into pre_goods_config(menu_id,goods_id,type,create_time) values($id,$type_id,2,$time)");
        }
        for ($i = 0; $i < count($type_3); $i++) {
            $type_id = (int)$type_3[$i];
            mysqli_query($con, "insert into pre_goods_config(menu_id,goods_id,type,create_time) values($id,$type_id,3,$time)");
        }

        mysqli_close($con);
        return out_right();

    }

    public function getList($requst)
    {
        date_default_timezone_set('Asia/Shanghai');
        $con = con();

        $name = $requst['name'];
        $page = (int)$requst['page'];
        $size = (int)$requst['size'];
        $start = ($page - 1) * $size;

        $where = ' where is_delete=1 ';
        if (!empty($name)) {
            $where .= " and name like '%$name%'";
        }
        $sql = "select * from pre $where";
        $data = mysqli_query($con, $sql . " order by `id` DESC limit $start,$size");
        $data = mysqli_fetch_all($data, true);
        $menu_ids = array_column($data, 'id');
        $menu_ids = implode(',', $menu_ids);
        if (empty($menu_ids)) {
            $goods = [];
            $total = 0;
        } else {
            $goods = mysqli_query($con, "select b.*,a.type,a.menu_id from pre_goods_config a left join goods b on (a.goods_id=b.id) where a.menu_id in ($menu_ids) and a.is_delete=1 and a.status=1");
            $goods = mysqli_fetch_all($goods, true);
            $total = mysqli_query($con, $sql);
            $total = mysqli_num_rows($total);
        }

        mysqli_close($con);
        for ($i = 0; $i < count($data); $i++) {
            $data[$i]['type_1'] = [];
            $data[$i]['type_2'] = [];
            $data[$i]['type_3'] = [];
            for ($o = 0; $o < count($goods); $o++) {
                if ($data[$i]['id'] == $goods[$o]['menu_id']) {
                    switch ($goods[$o]['type']) {
                        case 1:
                            $data[$i]['type_1'][] = $goods[$o];
                            break;
                        case 2:
                            $data[$i]['type_2'][] = $goods[$o];
                            break;
                        case 3:
                            $data[$i]['type_3'][] = $goods[$o];
                            break;
                    }
                }

            }
            if(empty($data[$i]['date'])){
                $data[$i]['date']=[];
            }else{
                $data[$i]['date']=explode(',', $data[$i]['date']);
            }
            $data[$i]['type_1_text'] =  array_column($data[$i]['type_1'], "name");
            $data[$i]['type_2_text'] = array_column($data[$i]['type_2'], 'name');
            $data[$i]['type_3_text'] = array_column($data[$i]['type_3'], 'name');

            $data[$i]['price_1']=sprintf("%.2f", $data[$i]['price_1']/100);
            $data[$i]['price_2']=sprintf("%.2f", $data[$i]['price_2']/100);
            $data[$i]['price_3']=sprintf("%.2f", $data[$i]['price_3']/100);
            $data[$i]['price_vip_1']=sprintf("%.2f", $data[$i]['price_vip_1']/100);
            $data[$i]['price_vip_2']=sprintf("%.2f", $data[$i]['price_vip_2']/100);
            $data[$i]['price_vip_3']=sprintf("%.2f", $data[$i]['price_vip_3']/100);

        }

        return out_right([
            'data' => $data,
            'page' => $page,
            'size' => $size,
            'total' => $total,
            'total_page' => ceil($total / $size)
        ]);
    }

    public function getGoods($request)
    {
        $menu_id = (int)$request['menu_id'];
        $goods_name = $request['goods_name'];
        $page = (int)$request['page'];
        $size = (int)$request['size'];
        $type = (int)$request['type'];
        $start = ($page - 1) * $size;

        $con = con();
        $sql = "select a.id,a.goods_id,b.`name`,b.first_price,b.price,b.vip_price,a.`status`,a.create_time,a.update_time from pre_goods_config a LEFT JOIN goods b on (a.goods_id=b.id)  where a.menu_id=$menu_id and type=$type  and a.is_delete=1 and b.is_delete=1 ";
        if (!empty($goods_name)) {
            $where = " and b.name like '%$goods_name%'";
        } else {
            $where = "";
        }
        $sql_0 = $sql . $where;
        $select = mysqli_query($con, "select * from goods y where NOT FIND_IN_SET(y.id, (select IFNULL(GROUP_CONCAT(x.goods_id SEPARATOR ','),0) from ($sql) x))");
        $select = mysqli_fetch_all($select, true);
        $goods = mysqli_query($con, $sql_0 . " ORDER BY a.create_time limit $start,$size");
        $goods = mysqli_fetch_all($goods, true);
        $total = mysqli_query($con, $sql_0);
        $total = mysqli_num_rows($total);
        mysqli_close($con);
        for ($i = 0; $i < count($goods); $i++) {
            $goods[$i]['price']=sprintf("%.2f", $goods[$i]['price']/100);
            $goods[$i]['create_time'] = date('Y-m-d H:i:s', $goods[$i]['create_time']);
            if (!empty($goods[$i]['update_time'])) {
                $goods[$i]['update_time'] = date('Y-m-d H:i:s', $goods[$i]['update_time']);
            }
        }
        return out_right([
            'data' => $goods,
            'page' => $page,
            'size' => $size,
            'total' => $total,
            'total_page' => ceil($total / $size),
            'select' => $select
        ]);
    }

    public function goodAdd($request)
    {
        $menu_id = (int)$request['menu_id'];
        $goods_id = $request['goods_id'];
        $type = (int)$request['type'];

        if(empty($goods_id)){
            return out_error('请传入商品！');
        }

        $con = con();
        $time = time();
        $date = date('Y-m-d');
        $res = mysqli_query($con, "select * from pre where id=$menu_id");
        $res = mysqli_fetch_all($res, true);
        if (count($res) == 0) {
            mysqli_close($con);
            return out_error('不存在此预订菜单！');
        }
        $menu_name = $res[0]['name'];
        $sql=[];
        for ($i = 0; $i < count($goods_id); $i++) {
            $id = (int)$goods_id[$i];
            $sql[]="($menu_id,$id,$type,$time)";
        }
        $sql=implode(',',$sql);
        mysqli_query($con, "insert into pre_goods_config(menu_id,goods_id,type,create_time) values$sql");

        //检测是否为今日菜单变动，若是，则执行今日菜单记录变动
        $today = mysqli_query($con, "select * from pre where id=$menu_id and `date` like '%$date%'");
        $today = mysqli_fetch_all($today, true);
        if (count($today) !== 0) {
            $old = mysqli_query($con, "select * from pre_day_record where type=$type and menu_id=$menu_id");
            $old = mysqli_fetch_all($old, true);
            if (count($old) != 0) {
                $old = array_column($old, 'goods_id');
                $add = array_diff($goods_id, $old);
            } else {
                $add = $goods_id;
            }

            if(!empty($add)){
                $add = implode(',', $add);
                $goods = mysqli_query($con, "select * from goods where  id in ($add)");
                $goods = mysqli_fetch_all($goods, true);
                $sql = [];
                for ($i = 0; $i < count($goods); $i++) {
                    $goods_id = (int)$goods[$i]['id'];
                    $goods_name = $goods[$i]['name'];
                    $url = $goods[$i]['img_url'];
                    $sql[] = "($menu_id,'$menu_name',$type,$goods_id,'$goods_name','$url',$time,$time,'当日预订菜单新增物品，自动添加至记录')";
                }
                $sql = implode(',', $sql);
                mysqli_query($con, "insert into pre_day_record(menu_id,menu_name,type,goods_id,goods_name,goods_url,create_time,update_time,remark) values$sql");
            }

        }


        mysqli_close($con);
        return out_right();
    }

    public function delGoods($request)
    {
        $id = (int)$request['id'];
        $con = con();
        $res = mysqli_query($con, "update pre_goods_config set is_delete=0 where id=$id");
        mysqli_close($con);
        if ($res) {
            return out_right();
        } else {
            return out_error('删除失败或数据已删除！');
        }
    }

}